﻿

----------------------------
--      Localization      --
----------------------------

----------------------------------
--      Local Declaration      --
----------------------------------

local pairs = pairs
local UnitExists = UnitExists
local UnitIsConnected = UnitIsConnected


----------------------------------
--      Module Declaration      --
----------------------------------
local modulePrototype = {}
modulePrototype.core = BunchOfBars


----------------------------------
--      Module Functions        --
----------------------------------

function modulePrototype:OnCreate(frame)
	-- do nothing
end
function modulePrototype:OnEnable(frame)
	-- do nothing
end

function modulePrototype:OnUpdate(frame, mypart)
	-- do nothing
end

function modulePrototype:OnInactive(frame, mypart)
	-- do nothing
end
function modulePrototype:UpdateAll()
	for _, frames in pairs(self.core.frames) do
		for unit,frame in pairs(frames) do
			if UnitExists(frame.unit) and frame.parts[self.moduleName] then
				self:OnUpdate(frame)
			end
		end
	end
end

function modulePrototype:UpdateAllWith(func)
	if type(func) == "function" then
		for _, frames in pairs(self.core.frames) do
			for _,frame in pairs(frames) do
				if UnitExists(frame.unit) and frame.parts[self.moduleName] then
					func(frame)
				end
			end
		end
	else -- let's just assume it's a strign as it should be
		for _, frames in pairs(self.core.frames) do
			for _,frame in pairs(frames) do
				if UnitExists(frame.unit) and frame.parts[self.moduleName] then
					self[func](self, frame)
				end
			end
		end
	end
end


function modulePrototype:UpdateUnits(_, units)
	for unit in pairs(units) do
		local head = "core"
		local unitid = unit
			if unit:find("target") then
				unit = unit:gsub("target","")
				head = "Target"
			elseif unit:find("pet") then
				unit = unit:gsub("pet","")
				head = "Pet"
			end
			if unit == "" then
				unit = "player"
			end
			if self.core.frames[head] then
				if self.core.frames[head][unit] and UnitExists(unit) then
					self:OnUpdate(self.core.frames[head][unit], unitid)
				end
			end
    	end
end
function modulePrototype:BarSize(size,p)
	if p == 0 then
		return 0.1
	else
		return size * p
	end
end
function modulePrototype:SetTextureCoord(vertical)
	if vertical then
		return {1,0,0,0,1,1,0,1}
	else
		return {0,0,0,1,1,0,1,1}
	end

end
function modulePrototype:SetBarPoint(bar, ltr, vertical)
	if ltr then
		if vertical then
			bar.front:SetPoint("TOPLEFT", bar.back)
			bar.front:SetPoint("TOPRIGHT", bar.back)
		else
			bar.front:SetPoint("TOPRIGHT", bar.back)
			bar.front:SetPoint("BOTTOMRIGHT", bar.back)
		end
	else
		if vertical then
			bar.front:SetPoint("BOTTOMLEFT", bar.back)
			bar.front:SetPoint("BOTTOMRIGHT", bar.back)
		else
			bar.front:SetPoint("TOPLEFT", bar.back)
			bar.front:SetPoint("BOTTOMLEFT", bar.back)
		end
	end
end

function modulePrototype:UpdateUnit(e, unit)
	local head = "core"
	local unitid = unit
	if unit:find("target") then
		unit = unit:gsub("target","")
		head = "Target"
	elseif unit:find("pet") then
		unit = unit:gsub("pet","")
		head = "Pet"
	end
	if unit == "" then
		unit = "player"
	end
	if self.core.frames[head] then
		if self.core.frames[head][unit] and UnitExists(unit) then
			self:OnUpdate(self.core.frames[head][unit], unitid)
		end
	end
end
BunchOfBars:SetDefaultModulePrototype(modulePrototype)
BunchOfBars:SetDefaultModuleLibraries("AceEvent-3.0")